package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
/*Request URL: http://localhost:8081/cart/add
Request Method: POST*/
public class CartController {
    @Autowired
    private CartService cartService;
    //添加购物车
    @PostMapping("/cart/add")
    public ResultInfo carAdd(@RequestBody Cart  cartParam){
//调用service添加
        Cart cart = cartService.cartAdd(cartParam);
        //返回购物车数据
        return ResultInfo.success(cart);
    }
    //修改购物车
    /*Request URL: http://localhost:8081/cart/sub
Request Method: POST*/
    //修改购物车
    @PostMapping("/cart/sub")
    public ResultInfo cartSub(@RequestBody Cart cartParam) {//接收参数（前端传递的参数较少，拿map集合接收）
        // 调用service删除
        Cart cart = cartService.cartSub(cartParam);
        //返回结果
        return ResultInfo.success(cart);
    }
    //查询购物车列表
    @GetMapping("/cart/list")
    public ResultInfo cartList() {
        //调用service查询
        List<Cart> cartList = cartService.cartList();
        //返回结果
        return ResultInfo.success(cartList);
    }

    //清空购物车
    /*Request URL: http://localhost:8081/cart/clean
Request Method: DELETE*/
    @DeleteMapping("/cart/clean")
    public ResultInfo cartClean() {
        //调用service删除
        cartService.cartClean();
        //返回成功
        return ResultInfo.success(null);
    }

}
